import 'package:equatable/equatable.dart';

abstract class AppException extends Equatable implements Exception {
  final String message;
  final int? code;
  final String? details;

  const AppException({
    required this.message,
    this.code,
    this.details,
  });

  @override
  List<Object?> get props => [message, code, details];
}

class NetworkException extends AppException {
  const NetworkException({
    required super.message,
    super.code,
    super.details,
  });
}

class TimeoutException extends AppException {
  const TimeoutException({
    required super.message,
    super.code,
    super.details,
  });
}

class CancelException extends AppException {
  const CancelException({
    required super.message,
    super.code,
    super.details,
  });
}

class AuthException extends AppException {
  const AuthException({
    required super.message,
    super.code,
    super.details,
  });
}

class ValidationException extends AppException {
  const ValidationException({
    required super.message,
    super.code,
    super.details,
  });
}

class CacheException extends AppException {
  const CacheException({
    required super.message,
    super.code,
    super.details,
  });
}

class StorageException extends AppException {
  const StorageException({
    required super.message,
    super.code,
    super.details,
  });
}

class ServerException extends AppException {
  const ServerException({
    required super.message,
    super.code,
    super.details,
  });
}

class PermissionException extends AppException {
  const PermissionException({
    required super.message,
    super.code,
    super.details,
  });
}

class FileException extends AppException {
  const FileException({
    required super.message,
    super.code,
    super.details,
  });
}

class UnknownException extends AppException {
  const UnknownException({
    required super.message,
    super.code,
    super.details,
  });
}

class ExceptionHandler {
  static AppException handleException(dynamic error) {
    if (error is AppException) {
      return error;
    }
    
    if (error is FormatException) {
      return ValidationException(
        message: '数据格式错误',
        details: error.message,
      );
    }
    
    if (error is ArgumentError) {
      return ValidationException(
        message: '参数错误',
        details: error.message,
      );
    }
    
    return UnknownException(
      message: '未知错误',
      details: error.toString(),
    );
  }
  
  static String getErrorMessage(AppException exception) {
    switch (exception.runtimeType) {
      case NetworkException:
        return exception.message.isNotEmpty ? exception.message : '网络连接失败';
      case TimeoutException:
        return exception.message.isNotEmpty ? exception.message : '请求超时';
      case CancelException:
        return exception.message.isNotEmpty ? exception.message : '请求已取消';
      case AuthException:
        return exception.message.isNotEmpty ? exception.message : '认证失败';
      case ValidationException:
        return exception.message.isNotEmpty ? exception.message : '数据验证失败';
      case CacheException:
        return exception.message.isNotEmpty ? exception.message : '缓存操作失败';
      case StorageException:
        return exception.message.isNotEmpty ? exception.message : '存储操作失败';
      case ServerException:
        return exception.message.isNotEmpty ? exception.message : '服务器错误';
      case PermissionException:
        return exception.message.isNotEmpty ? exception.message : '权限不足';
      case FileException:
        return exception.message.isNotEmpty ? exception.message : '文件操作失败';
      default:
        return exception.message.isNotEmpty ? exception.message : '未知错误';
    }
  }
}